#!/usr/bin/env bash

icon() {
    if [[ "$(awk '/%/ {gsub(/[\[\]]/,""); print $6}' <(amixer sget Master) | awk 'FNR == 1')" == "on" ]]; then

        # search for the lines containing 'Right:' or 'Mono:', when more than 1 field exists
        # we strip the trailing '%' and round it up with printf "%0.0f" just in case
        vol=$(amixer sget Master | awk -F'[][]' '/Right:|Mono:/ && NF > 1 {sub(/%/, ""); printf "%0.0f\n", $2}')
        # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
        if [[ "${vol}" -ge 80 ]]; then
            echo ""
        elif [[ "${vol}" -ge 40 ]]; then
            echo "奔"
        elif [[ "${vol}" -ge 10 ]]; then
            echo ""
        else
            echo ""
        fi
    else
        vol=$(amixer sget Master | awk -F'[][]' '/Right:|Mono:/ && NF > 1 {sub(/%/, ""); printf "%0.0f\n", $2}')
        # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
        if [[ "${vol}" -ge 10 ]]; then
            echo "󰖁"
        else
            echo ""
        fi
        
    fi
}

getvol() {
    vol=$(amixer sget Master | awk -F'[][]' '/Right:|Mono:/ && NF > 1 {sub(/%/, ""); printf "%0.0f\n", $2}')
    echo $vol
}

if [[ $1 == "--icon" ]]; then
    icon
elif [[ $1 == "--value" ]]; then
    getvol | tail -1
fi
